package com.sgb.kjwl.view.ui.adapter.pick;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.pick.TenderBidderListEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

public class PickHomeHotAdapter extends BaseRecycleAdapter<TenderBidderListEntity> {

    private String type = "0";

    private RecyclerView recyclerView;
    private LinearLayout ll_gc;
    private LinearLayout ll_goods;
    private TextView name1;
    private IChildClick iChildClick;
    private LinearLayout noList;
    private PickHomeHotChildHeadAdapter pickHomeHotChildHeadAdapter;

    public PickHomeHotAdapter(Context context, List<TenderBidderListEntity> datas) {
        super(context, R.layout.item_pick_hall_biding, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final TenderBidderListEntity entity) {
        holder.setBinding(BR.viewModel, entity);

        recyclerView = holder.itemView.findViewById(R.id.child_rv);
        ll_gc = holder.itemView.findViewById(R.id.ll_gc);
        ll_goods = holder.itemView.findViewById(R.id.ll_goods);
        name1 = holder.itemView.findViewById(R.id.tv_name1);
        noList = holder.itemView.findViewById(R.id.no_list);

        if (entity.getGoodsHeaders() == null || entity.getGoodsLists() == null) {
            recyclerView.setVisibility(View.GONE);
            noList.setVisibility(View.VISIBLE);
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            noList.setVisibility(View.GONE);
            pickHomeHotChildHeadAdapter = new PickHomeHotChildHeadAdapter(context, entity.getGoodsHeaders(), entity.getGoodsLists());
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
            recyclerView.setAdapter(pickHomeHotChildHeadAdapter);
        }

        TextView tvType2 = holder.itemView.findViewById(R.id.tv_type2);
        ImageView imgTab = holder.itemView.findViewById(R.id.img_tab);
        TextView tvType3 = holder.itemView.findViewById(R.id.tv_type3);
        TextView tvEndTime = holder.itemView.findViewById(R.id.tv_endTime);
        TextView name = holder.itemView.findViewById(R.id.name);
        TextView tvType = holder.itemView.findViewById(R.id.tv_type);

        // 招标方式：1、资格预审；2、资格后审；3、报价即报名
        if(entity.getVerticalTypeValue() > 0){
            tvType.setVisibility(View.VISIBLE);
        }else{
            tvType.setVisibility(View.GONE);
        }
        if (1 == entity.getVerticalTypeValue()) {
            tvType.setText("资格预审");
            tvType.setBackgroundResource(R.drawable.bg_purple);
        } else if (2 == entity.getVerticalTypeValue()){
            tvType.setText("资格后审");
            tvType.setBackgroundResource(R.drawable.bg_orange);
        }else if (3 == entity.getVerticalTypeValue()){
            tvType.setBackgroundResource(R.drawable.bg_price);
            tvType.setText("报名即报价");
        }

        imgTab.setVisibility(View.VISIBLE);
        switch (entity.getState()) {
            case 2:
            case 6:
                tvEndTime.setText(String.format("投标结束时间：%s", TextUtils.isEmpty(entity.getEndTimes()) ? "" : entity.getEndTimes()));
                imgTab.setBackground(context.getResources().getDrawable(R.mipmap.icn_zbz));
                break;
            case 9:
                tvEndTime.setText(String.format("报名结束时间：%s", TextUtils.isEmpty(entity.getApplyEndTime()) ? "" : entity.getApplyEndTime()));
                imgTab.setBackground(context.getResources().getDrawable(R.drawable.pick_notice_bmz));
                break;
            case 8:
                tvEndTime.setText(String.format("报名开始时间：%s", TextUtils.isEmpty(entity.getApplyStartTime()) ? "" : entity.getApplyStartTime()));
                imgTab.setBackground(context.getResources().getDrawable(R.drawable.pick_notice_dbm));
                break;
            default:
                imgTab.setVisibility(View.GONE);
                break;
        }

        name.setText(entity.getName());

        if (type.equals("0")) {
            tvType2.setText(entity.getTypeName());
            tvType2.setVisibility(View.VISIBLE);
        } else {
            tvType2.setText("");
            tvType2.setVisibility(View.GONE);
        }

        //新增 “招标轮次”     “报名中”9、“待招标”1状态不显示，其他状态显示    接口判断
        TextView textViewWheel = holder.itemView.findViewById(R.id.tv_wheel_num);
        if (entity.getRotation() == 0 || entity.getState() == 1 || entity.getState() == 8 || entity.getState() == 9) {
            textViewWheel.setVisibility(View.GONE);
        } else {
            textViewWheel.setVisibility(View.VISIBLE);
            textViewWheel.setText("第" + entity.getRotation() + "轮");
        }

//        //增加招标方式：公开招标、邀请招标
//        String biddingMethodName = entity.getBiddingMethodName();
//        if(!TextUtils.isEmpty(biddingMethodName)){
//            tvType3.setVisibility(View.VISIBLE);
//            if ("公开招标".equals(biddingMethodName)){
//                tvType3.setBackground(context.getResources().getDrawable(R.drawable.bg_green));
//            }else{
//                tvType3.setBackground(context.getResources().getDrawable(R.drawable.bg_orange));
//            }
//            tvType3.setText(entity.getBiddingMethodName());
//        }

        if (type.equals("0")) {
            tvType2.setText(entity.getTypeName());
            tvType2.setVisibility(View.VISIBLE);
        } else {
            tvType2.setText("");
            tvType2.setVisibility(View.GONE);
        }

        pickHomeHotChildHeadAdapter.setOnItemClick(new PickHomeHotChildHeadAdapter.OnItemClick() {
            @Override
            public void onItemClick() {
                iChildClick.onClick(entity);
            }
        });
    }

    public void setType(String typeNo) {
        type = typeNo;
    }

    public void onIChildClick(IChildClick iChildClick) {
        this.iChildClick = iChildClick;
    }

    public interface IChildClick {
        void onClick(TenderBidderListEntity tenderBidderListEntity);
    }

}
